package com.xinqi.modules.exam.paper.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.xinqi.common.base.error.CommonErrors;
import com.xinqi.common.mybatis.data.service.impl.BaseServiceImpl;
import com.xinqi.common.mybatis.utils.Queries;
import com.xinqi.common.redis.utils.Caches;
import com.xinqi.modules.exam.common.constants.ExamConstants;
import com.xinqi.modules.exam.paper.domain.CourseExamPaperConfigEntity;
import com.xinqi.modules.exam.paper.domain.CourseExamPaperEntity;
import com.xinqi.modules.exam.paper.dto.req.CourseExamPaperConfigQueryDTO;
import com.xinqi.modules.exam.paper.dto.rsp.CourseExamPaperConfigResultDTO;
import com.xinqi.modules.exam.paper.manager.CourseExamPaperConfigManager;
import com.xinqi.modules.exam.paper.mapper.CourseExamPaperConfigMapper;
import lombok.RequiredArgsConstructor;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 课程测评配置管理层实现
 *
 * @author: linkai
 * @date: 2023/07/02
 */
@Service
@RequiredArgsConstructor
public class CourseExamPaperConfigManagerImpl extends BaseServiceImpl<CourseExamPaperConfigMapper, CourseExamPaperConfigEntity> implements CourseExamPaperConfigManager {
    private final RedissonClient redissonClient;

    /**
     * 自定义分页查询
     */
    @Override
    public Page<CourseExamPaperConfigResultDTO> findPage(Page<Object> page, CourseExamPaperConfigQueryDTO query) {
        return this.baseMapper.findPage(page, query);
    }

    /**
     * 自定义列表查询
     */
    @Override
    public List<CourseExamPaperConfigResultDTO> findList(CourseExamPaperConfigQueryDTO query) {
        return this.baseMapper.findList(query);
    }

    /**
     * 根据id查询 课程测评配置数据
     */
    @Override
    public CourseExamPaperConfigEntity findById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        Long examId = findExamIdById(id);
        return findByExamId(examId);
    }

    @Override
    public CourseExamPaperConfigEntity findByExamId(Long examId) {
        if (examId == null) {
            return null;
        }
        return Caches.get(() -> {
            LambdaQueryWrapper<CourseExamPaperConfigEntity> wrapper = Queries.lambda();
            wrapper.eq(CourseExamPaperConfigEntity::getExamPaperId, examId);
            return this.baseMapper.selectOne(wrapper);
        }, redissonClient, ExamConstants.EXAM_CONFIG_EID_KEY, examId);
    }

    /**
     * 根据id查询 课程测评配置数据
     */
    @Override
    public Long findExamIdById(Long id) {
        if (id == null) {
            return null;
        }
        return Caches.get(() -> this.baseMapper.findExamIdById(id),
            redissonClient, ExamConstants.EXAM_CONFIG_ID_TO_EID_KEY, id);
    }

    /**
     * 根据id更新 课程测评配置数据，重写
     */
    @Override
    public boolean updateById(CourseExamPaperConfigEntity entity) {
        boolean b = SqlHelper.retBool(getBaseMapper().updateById(entity));
        if (b) {
            Long examId = findExamIdById(entity.getId());
            try {
                // 清理缓存
                Caches.del(redissonClient, ExamConstants.EXAM_CONFIG_EID_KEY, examId);
            } catch (Exception ignored) {
            }
        }
        return b;
    }

    /**
     * 根据id删除 课程测评配置数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        Long examId = findExamIdById(id);
        if (examId == null) {
            return false;
        }
        boolean b = this.removeById(id);
        if (b) {
            try {
                // 清理缓存
                Caches.del(redissonClient, ExamConstants.EXAM_CONFIG_EID_KEY, examId);
            } catch (Exception ignored) {
            }
        }
        return b;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void initConfig(CourseExamPaperEntity entity) {
        CourseExamPaperConfigEntity config = new CourseExamPaperConfigEntity();
        config.setExamPaperId(entity.getId());
        this.baseMapper.insert(config);
    }

}
